package study.eggs.validation.validator;

import lombok.extern.slf4j.Slf4j;
import study.eggs.exception.param.EmptyValueException;
import study.eggs.validation.constraints.NotEmpty;

import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Objects;

/**
 * 非空验证器
 * （1）String
 * （2）List Set
 * （3）Integer Long BigDecimal
 * （4）Pojo
 */
@Slf4j
public class NotEmptyValidator implements Validator {

    @Override
    public void validate(String name, Object value, Annotation annot) {
        NotEmpty notEmpty = (NotEmpty) annot;
        //非空
        if (Objects.isNull(value) ||
                (value instanceof String && ((String) value).length() == 0) ||
                (value instanceof Collection && ((Collection) value).size() == 0)) {
            throw new EmptyValueException(name);
        }
    }
}
